package com.sean.common.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.sean.common.exception.ServiceException;
import com.sean.common.page.PageRowBounds;
import com.sean.common.page.Pagination;
import com.sean.common.page.ResultPagination;


/**
 * 服务基础类.
 * 
 * @author XinWang
 * 
 */
public abstract class BaseService {

	protected static final Log log = LogFactory.getLog(BaseService.class);

	@Autowired
	private SqlSessionFactory sqlSessionFactory;

	/**
	 * 分页查询.
	 * 
	 * @param InterfaceClass
	 * @param MethodName
	 * @param Pagination
	 * @param ResultClass
	 * @return
	 */
	public <T> ResultPagination<T> selectListForPage(Class mapperClass, String methodName, Pagination pagination,
			Class<T> resultClass) {
		return this.selectListForPage(mapperClass.getName() + "." + methodName, pagination, resultClass);
	}

	/**
	 * 分页查询.
	 * 
	 * @param statement
	 *            查询使用的mapper方法.
	 * @param pagination
	 *            查询范围.
	 * @param resultClass
	 *            返回实体类型.
	 * @return
	 */
	public <T> ResultPagination<T> selectListForPage(String statement, Pagination pagination, Class<T> resultClass) {
		return this.selectListForPage(statement, pagination.getParamMap(), new PageRowBounds(pagination), resultClass);
	}

	/**
	 * 分页查询.
	 * 
	 * @param statement
	 *            查询使用的mapper方法.
	 * @param parameters
	 *            查询参数.
	 * @param currentPage
	 *            跳转页数.
	 * @param clazz
	 *            返回实体类型.
	 * @return
	 */
	public <T> ResultPagination<T> selectListForPage(String statement, Map parameters, int currentPage,
			Class<T> resultClass) {
		return this.selectListForPage(statement, parameters, new PageRowBounds(currentPage), resultClass);
	}
	/**
	 * 分页查询
	 * @param statement
	 * @param parameters
	 * @param rowBounds
	 * @param resultClass
	 * @return
	 */
	private <T> ResultPagination<T> selectListForPage(String statement, Map parameters, PageRowBounds rowBounds,
			Class<T> resultClass) {
		SqlSession session = null;
		List<T> list = null;
		try {
			session = sqlSessionFactory.openSession();
			if (parameters == null) {
				parameters = new HashMap();
			}
			list = session.selectList(statement, parameters, rowBounds);
			return new ResultPagination<T>(rowBounds, parameters, list);
		} catch (Exception e) {
			log.error("分页查询失败.", e);
			throw new ServiceException("分页查询失败!");
		} finally {
			session.close();
		}
	}
	
	/**
	 * 获取查询Map
	 * @param keyValue
	 * @return
	 */
	protected Map<String, Object> getParamMap(Object...keyValue) {
		if(keyValue.length%2 != 0) {
			throw new ServiceException("参数必须为Key对应Value");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		for (int i=0;i<keyValue.length;i+=2) {
			paramMap.put((String) keyValue[i], keyValue[i+1]);
		}
		return paramMap;
	}
}
